#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void log_message(const char *message) {
    printf("%s\n", message);
}

void create_service_file() {
    const char *service_file_path = "/lib/systemd/system/rabbitmq.service";
    log_message("Creating RabbitMQ service file...");
    FILE *service_file = fopen(service_file_path, "w");

    if (service_file == NULL) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    const char *service_content =
            "[Unit]\n"
            "Description=RabbitMQ broker\n"
            "After=network.target epmd@0.0.0.0.socket\n"
            "Wants=network.target epmd@0.0.0.0.socket\n"
            "\n"
            "[Service]\n"
            "Type=notify\n"
            "User=root\n"
            "Group=root\n"
            "NotifyAccess=all\n"
            "TimeoutStartSec=3600\n"
            "WorkingDirectory=/opt/nancal/base/rabbitmq\n"
            "ExecStart=/opt/nancal/base/rabbitmq/bin/rabbitmq-server\n"
            "ExecStop=/opt/nancal/base/rabbitmq/bin/rabbitmqctl stop\n"
            "ExecStop=/bin/sh -c \"while ps -p $MAINPID >/dev/null 2>&1; do sleep 1; done\"\n"
            "\n"
            "[Install]\n"
            "WantedBy=multi-user.target\n";

    fprintf(service_file, "%s", service_content);
    fclose(service_file);
    log_message("RabbitMQ service file created successfully.");
}

void run_command(const char *command, const char *message) {
    log_message(message);
    int ret = system(command);
    if (ret != 0) {  // Check if the command was successful
        fprintf(stderr, "Error executing command: %s\n", command);
        exit(EXIT_FAILURE);
    }
}

int main() {
    const char *libcrypto_path = "/usr/lib64/libcrypto.so.10";

    // 检查 libcrypto.so.10 文件是否存在
    if (access(libcrypto_path, F_OK) != 0) {
        run_command("cp libcrypto.so.1.0.0 /usr/lib64/libcrypto.so.10", "Copying libcrypto.so.1.0.0 to /usr/lib64/libcrypto.so.10...");
    } else {
        log_message("libcrypto.so.10 already exists, skipping copy.");
    }

    // 安装 RabbitMQ RPM 包
    run_command("rpm -ivh rabbitmq-3.9.11-9.x86_64.rpm --nodeps", "Installing RabbitMQ RPM package...");

    create_service_file();

    run_command("systemctl daemon-reload", "Reloading systemd daemon...");
    run_command("systemctl start rabbitmq", "Starting RabbitMQ service...");
    run_command("systemctl status rabbitmq", "Checking RabbitMQ service status...");
    run_command("systemctl enable rabbitmq", "Enabling RabbitMQ service to start on boot...");

    log_message("RabbitMQ deployment completed successfully.");

    // install redis
    run_command("rpm -ivh redis-6.2.6-1.x86_64.rpm --nodeps", "Installing Redis RPM package...");
    run_command("echo 'requirepass 1@#4' >> /opt/nancal/base/redis/redis.conf", "set password to config");

    run_command("systemctl start redisd", "Starting Redis service...");
    run_command("systemctl status redisd", "Checking Redis service status...");
    run_command("systemctl enable redisd", "Enabling Redis service to start on boot...");

    // install openresty
    run_command("rpm -ivh openresty-1.21.4.1-1.el7.x86_64.rpm --nodeps", "Installing Openresty RPM package...");

    run_command("systemctl start openresty", "Starting openresty service...");
    run_command("systemctl status openresty", "Checking openresty service status...");
    run_command("systemctl enable openresty", "Enabling openresty service to start on boot...");

    // install minio
    run_command("rpm -ivh minio-1-RELEASE.20211013.x86_64.rpm --nodeps", "Installing Minio RPM package...");

    run_command("systemctl start miniod.service", "Starting miniod service...");
    run_command("systemctl status miniod", "Checking miniod service status...");
    run_command("systemctl enable miniod", "Enabling miniod service to start on boot...");


    return 0;
}
